﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace QuanLyThuVien.DAL
{
    class daoNguoiDung
    {
        Connection cn = new Connection();

        public DataTable ChonDanhSachNguoiDung(String NguoiDung)
        {
            SqlParameter[] Parameters = new SqlParameter[1];
            Parameters[0] = new SqlParameter("@NguoiDung", SqlDbType.VarChar, 50);
            Parameters[0].Value = NguoiDung;
            return cn.executeSelectProcedureQuery("ChonDanhSachNguoiDung", Parameters);
        }

        public DataTable ChonNguoiDung(String NguoiDung)
        {
            SqlParameter[] Parameters = new SqlParameter[1];
            Parameters[0] = new SqlParameter("@NguoiDung", SqlDbType.VarChar, 50);
            Parameters[0].Value = NguoiDung;
            return cn.executeSelectProcedureQuery("ChonNguoiDung", Parameters);
        }

        public bool DangKy(String NguoiDung, String MatKhau)
        {
            SqlParameter[] Parameters = new SqlParameter[2];
            Parameters[0] = new SqlParameter("@NguoiDung", SqlDbType.VarChar, 50);
            Parameters[0].Value = NguoiDung;
            Parameters[1] = new SqlParameter("@MatKhau", SqlDbType.VarChar, 50);
            Parameters[1].Value = MatKhau;
            return cn.executeInsertProcedureQuery("DangKy", Parameters);
        }

        public bool SuaQuyenNguoiDung(String NguoiDung,
                            bool QuyenQuanLyNguoiDung,
                            bool QuyenQuanLyDocGia,
                            bool QuyenQuanLySach,
                            bool QuyenQuanLyThongTinMuonTra)
        {
            SqlParameter[] Parameters = new SqlParameter[5];
            Parameters[0] = new SqlParameter("@NguoiDung", SqlDbType.VarChar, 50);
            Parameters[0].Value = NguoiDung;
            Parameters[1] = new SqlParameter("@QuyenQuanLyNguoiDung", SqlDbType.Bit);
            Parameters[1].Value = QuyenQuanLyNguoiDung;
            Parameters[2] = new SqlParameter("@QuyenQuanLyDocGia", SqlDbType.Bit);
            Parameters[2].Value = QuyenQuanLyDocGia;
            Parameters[3] = new SqlParameter("@QuyenQuanLySach", SqlDbType.Bit);
            Parameters[3].Value = QuyenQuanLySach;
            Parameters[4] = new SqlParameter("@QuyenQuanLyThongTinMuonTra", SqlDbType.Bit);
            Parameters[4].Value = QuyenQuanLyThongTinMuonTra;
            return cn.executeUpdateProcedureQuery("SuaQuyenNguoiDung", Parameters);
        }

        public bool ThayMatKhau(String NguoiDung, String MatKhau)
        {
            SqlParameter[] Parameters = new SqlParameter[2];
            Parameters[0] = new SqlParameter("@NguoiDung", SqlDbType.VarChar, 50);
            Parameters[0].Value = NguoiDung;
            Parameters[1] = new SqlParameter("@MatKhau", SqlDbType.VarChar, 50);
            Parameters[1].Value = MatKhau;
            return cn.executeUpdateProcedureQuery("ThayMatKhau", Parameters);
        }

        public bool XoaNguoiDung(String NguoiDung)
        {
            SqlParameter[] Parameters = new SqlParameter[1];
            Parameters[0] = new SqlParameter("@NguoiDung", SqlDbType.VarChar, 50);
            Parameters[0].Value = NguoiDung;
            return cn.executeDeleteProcedureQuery("XoaNguoiDung", Parameters);
        }

        public DataSet DanhSachNguoiDung(String NguoiDung)
        {
            DataSet dts = new DataSet();
            dts = cn.getDataSet("Select * From NguoiDung Where NguoiDung not like '" + NguoiDung + "'"
                            + "and NguoiDung not like 'admin'", "NguoiDung");
            dts.Tables["NguoiDung"].Columns["NguoiDung"].ReadOnly = false;
            dts.Tables["NguoiDung"].Columns["MatKhau"].ReadOnly = false;
            return dts;
        }

        public void CapNhatNguoiDung()
        {
            cn.updateServerByDataSet("NguoiDung");
        }
    }
}
